<template>
  <div class="dashboard-container">
    <h2 class="head-big-title">Dealer</h2>
    <Pathnavigation style="margin-left: 3%;"></Pathnavigation>
    <el-button
      round
      v-show="role != dealerMValue"
      type="info"
      size="medium"
      style="float: left;margin-left: 3%;margin-bottom: 10px;"
      @click="deleteArray">Padam</el-button>
    <el-form label-position="top" label-width="80px" :model="searchData" style="width: 92%;margin: 20px 4% 0 4%;padding: 10px 2% 3px 2%;border-radius: 10px;background-color: white;text-align: left;clear: both;">
      <el-form-item label="Nama Pelanggan" class="one-form-item">
        <el-input v-model="searchData.name" class="input-width" @change="getResetLoadData" placeholder="Nama Pelanggan"></el-input>
      </el-form-item>
      <el-form-item label="status" class="one-form-item">
        <el-select v-model="searchData.type" class="input-width" @change="getResetLoadData" placeholder="status">
          <el-option label="Semua" value=""></el-option>
          <el-option label="Mohon" value="0"></el-option>
          <el-option label="Lulus" value="1"></el-option>
          <el-option label="Dilarang" value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div style="width: 96%;margin: 20px 2% 0 2%;padding-left: 10px;border-radius: 20px;background-color: rgb(245,246,247);text-align: left;">
      <div v-for="(item,index) in tableData" :key="index" style="width: 100%;border-radius: 10px;background-color: white;padding: 6px 5px 15px 6px;margin-bottom: 10px;">
        <el-checkbox v-model="item.choosed" :true-label="1" :false-label="0" v-if="!(role == dealerMValue)" style="float: left;font-size: 20px;"></el-checkbox>
        <div style="width: 10px;height: 2px;"></div>
        <svg-icon icon-class="ico_view" style="font-size: 24px;float: right;margin-right: 5%;position: relative;bottom: 3px;" @click="handleEdit(item)"></svg-icon>
        <el-divider></el-divider>
        <div style="width: 100%;">
          <div style="width: 60px;height: 60px;display: inline-block;">
            <el-image
              v-if="item.typeCompany"
              style="width: 52px; height: 52px;"
              :src="picbase + item.logoUrl"></el-image>
            <el-image
              v-else
              style="width: 52px; height: 52px;"
              :src="picbase + item.certificateUrl"></el-image>
          </div>
          <div style="display: inline-block;width: 60%;height: 60px;line-height: 32px;">
            <strong>Dealer Name</strong>
            <p>{{item.dealerName}}</p>
          </div>
        </div>
        <div style="width: 100%;">
          <div style="width: 50%;display: inline-block;">
            <strong>Registration Number</strong>
            <p>{{item.registCode}}</p>
          </div>
          <div style="width: 49%;display: inline-block;">
            <strong>Phone Number</strong>
            <p>{{item.phoneNumber}}</p>
          </div>
        </div>
        <div style="width: 100%;display: inline-block;line-height: 24px;">
          <strong>Status</strong>
          <p v-if="item.state == 0">Approved</p>
          <p v-else-if="item.state == 1">Approved</p>
          <p v-else-if="item.state == 2">Rejected</p>
          <p v-else>Null</p>
        </div>
      </div>
    </div>
    <el-pagination
      style="float: right;margin: 20px 4% 20px 0;"
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :current-page="searchData.pageIndex"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { query_dealerDealerApi, passOwnerDealerApi, deleteDealerApi } from '@/api/user'

export default {
  name: 'Customers',
  computed: {
    ...mapGetters([
      'name',
      'role'
    ])
  },
  data() {
    return {
      tableData: [],
      searchData: {
        pageNum: 10,
        pageIndex: 1
      },
      willDel: [],
      orderChart: '',
      total: 0
    }
  },
  mounted() {
    this.$removeDetail()
    this.loadData()
  },
  methods: {
    getResetLoadData() {
      this.searchData.pageIndex = 1
      this.loadData()
    },
    getMainCustomerId() {
      return localStorage.getItem('mainCustomerId') ? localStorage.getItem('mainCustomerId') : null
    },
    changeNew() {
      this.$forceUpdate()
    },
    handleCurrentChange(val) {
      this.searchData.pageIndex = val
      this.loadData()
    },
    handleConfirm(row) {
      passOwnerDealerApi(row).then(res => {
        this.$message({
          message: 'Pass！',
          type: 'success'
        })
        this.loadData()
      }).catch(err => {
        console.log(err)
      })
    },
    loadData() {
      if (this.getMainCustomerId()) {
        this.searchData.id = this.getMainCustomerId()
      }
      query_dealerDealerApi(this.searchData).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      }).catch(err => {
        console.log(err)
      })
    }, // 得到数据表格
    handleEdit(row) {
      this.$setDetail(row)
      this.$router.push({ path: '/Customers/Detail' })
    },
    deleteArray() {
      this.$confirm('Padam Data', 'Tips', {
        confirmButtonText: 'Yes',
        cancelButtonText: 'No',
        type: 'warning'
      }).then(() => {
        deleteDealerApi(this.willDel).then(res => {
          this.$message({
            message: 'Berjaya',
            type: 'success'
          })
          this.loadData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: 'Cancel'
        })
      })
    },
    handleSelectionChange(val) {
      this.willDel = []
      if (val) {
        if (val.length) {
          if (val.length > 0) {
            let newArray = {}
            for (let i = 0; i < val.length; i++) {
              newArray = val[i].id
              this.willDel.push(newArray)
            }
          }
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .head-big-title{
    text-align: left;
    font-size: 20px;
    margin: 20px 0 0 3%;
  }
  .dashboard-container{
    margin-top: 20px;
    text-align: center;
  }
  .one-form-item{
    display: block;
    .el-select{
      width: 100%;
    }
    .el-input{
      width: 100%;
    }
  }
</style>
<style scoped>
  /deep/.head-search label{
    line-height: 30px;
  }
  /deep/.el-divider{
    margin: 28px 0 10px 0;
  }
</style>
